using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ToDoListWeb.Service;

namespace ToDoListWeb.Controllers
{
    public class TypeController : Controller
    {
        TypeService _type = new TypeService();
        UserService _user = new UserService();

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 删除类型及类型下所有清单、事项
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public IActionResult DeleteType(int typeId)
        {
            var _aUser = _user.GetUserModel((int)(HttpContext.Session.GetInt32("user")));
            _type.DeleteType(typeId);
            _user.UpdateUserModel(_aUser);
            return View(_aUser);
        }
        /// <summary>
        /// 编辑类型（方法）
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public IActionResult CompileType(int typeId,string typeName)
        {
            var _aUser = _user.GetUserModel((int)(HttpContext.Session.GetInt32("user")));
            if (typeId == 0 || typeName == null)
                View("ShowEvents", _aUser);
            _type.CompileType(typeId,typeName);
            _user.UpdateUserModel(_aUser);
            return View("ShowEvents", _aUser);
        }
        /// <summary>
        /// 编辑类型（视图）
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public IActionResult CompileTypeView(int typeId)
        {
            var type = _type.GetType(typeId);
            return View(type);
        }

        /// <summary>
        /// 显示添加清单类型的输入框
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTypeView()
        {
            var _aUser = _user.GetUserModel((int)(HttpContext.Session.GetInt32("user")));
            _user.UpdateUserModel(_aUser);
            return View(_aUser);
        }
        /// <summary>
        /// 添加清单类型,从输入框中读取输入的清单类型，然后添加清单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult FoundType(string typeName)
        {
            var _aUser = _user.GetUserModel((int)(HttpContext.Session.GetInt32("user")));
            if (typeName != null)
            {
                _type.CreateType(_aUser, typeName);
                _user.UpdateUserModel(_aUser);
            }
            return View(_aUser);
        }
    }
}